﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using Microsoft.Practices.Composite.Events;
using Razre;
using System.Windows.Media.Animation;
using System.Windows.Controls;

namespace NavigationMVVMDemo
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        #region Properties

        public static App Current
        {
            get
            {
                return Application.Current as App;
            }
        }

        private EventAggregator _eventAggregator;
        public EventAggregator EventAggregator
        {
            get
            {
                if (_eventAggregator == null)
                    _eventAggregator = new EventAggregator();

                return _eventAggregator;
            }
        }

        #endregion

        #region overrides

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            Settings.EventAggregator = EventAggregator;

            ThemeManager.LoadTheme(ApplicationDefinitions.ThemeNames.Theme1, ApplicationDefinitions.ThemeURIs.Theme1);
            ThemeManager.LoadTheme(ApplicationDefinitions.ThemeNames.Theme2, ApplicationDefinitions.ThemeURIs.Theme2);

            ThemeManager.SetTheme(ApplicationDefinitions.ThemeNames.Theme1);

            NavigationManager.OnRequestTransitionAnimation += new EventHandler<RequestTransitionAnimationEventArgs>(NavigationManager_OnRequestTransitionAnimation);

            //NavigationManager.CurrentViewAnimation.Add(ApplicationDefinitions.TransitionAnimations.BuildFadeOutAnimation);
            //NavigationManager.NextViewAnimation.Add(ApplicationDefinitions.TransitionAnimations.BuildFadeInAnimation);

        }

        void NavigationManager_OnRequestTransitionAnimation(object sender, RequestTransitionAnimationEventArgs e)
        {
            e.NextView.Opacity = 0;

            Storyboard sb = new Storyboard();

            if (e.CurrentView != null)
            {
                DoubleAnimation fadeOut = ApplicationDefinitions.TransitionAnimations.BuildFadeOutAnimation();
                Storyboard.SetTarget(fadeOut, e.CurrentView);

                sb.Children.Add(fadeOut);
            }

            DoubleAnimation fadeIn = ApplicationDefinitions.TransitionAnimations.BuildFadeInAnimation();
            Storyboard.SetTarget(fadeIn, e.NextView);            
            
            sb.Children.Add(fadeIn);

            e.Result = sb;

        }

        #endregion

        #region methods

        
        #endregion


    }
}
